package com.example.qixin0314.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.qixin0314.Nat.BaseApplication;
import com.example.qixin0314.Nat.GlideImageLoader;
import com.example.qixin0314.Nat.newsData.Values1;
import com.example.qixin0314.Nat.newsData.newdata;
import com.example.qixin0314.Nat.newsimage.NewsData;
import com.example.qixin0314.Nat.newsimage.Values;
import com.example.qixin0314.Activity.NewsActivity;
import com.example.qixin0314.R;
import com.example.qixin0314.bean.imagesModel;
import com.example.qixin0314.bean.newsAdapter;
import com.example.qixin0314.bean.newsModel;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class newsFragment extends Fragment {
    private Banner banner;
    private RecyclerView listview;
    private newsAdapter adapter;
    private List<newsModel>newsData;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_news, container, false);
        initView(view);//控件初始化
        RecycleAddData();//新闻列表
        Carousel();//轮播图


        return view;
    }

    private void initView(View view) {
        banner = view.findViewById(R.id.banner);
        //1.获取listview
        listview = view.findViewById(R.id.list_view);
        listview.smoothScrollToPosition(0);//控制滑动速度
        //initRefresh();
        initRefresh(view);




    }

    private void Carousel() {//轮播图

        List <imagesModel>models=new ArrayList();
        Call<NewsData<Values>> picture = BaseApplication.httpbinService.Picture();
        picture.enqueue(new Callback<NewsData<Values>>() {
            @Override
            public void onResponse(Call<NewsData<Values>> call, Response<NewsData<Values>> response) {
                for (int i = 0; i < response.body().getValues().getHot_baodao().size(); i++) {
                    imagesModel it=new imagesModel();
                    it.setId(response.body().getValues().getHot_baodao().get(i).getId());
                    it.setName(response.body().getValues().getHot_baodao().get(i).getTitle());
                    it.setImages(response.body().getValues().getHot_baodao().get(i).getUrl());
                    models.add(it);
                }
                //设置图片加载器
                banner.setImageLoader(new GlideImageLoader());
                //设置图片集合
                List images = new ArrayList();
                for (int i = 0; i < models.size(); i++) {
                    images.add(models.get(i).getImages());
                }
                banner.setImages(images);
                //banner设置方法全部调用完毕时最后调用
                banner.start();
            }

            @Override
            public void onFailure(Call<NewsData<Values>> call, Throwable t) {
            }
        });

        //增加点击事件
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(getActivity(), "position"+position, Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(getActivity(), NewsActivity.class);
                intent.putExtra("name",models.get(position).getName());
                intent.putExtra("uri","http://android.qxswt.com/mediadetail/"+models.get(position).getName());
                startActivity(intent);
            }
        });
    }

    private void RecycleAddData(){
        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        listview.setLayoutManager(llm);
        newsData=new ArrayList<>();
        adapter=new newsAdapter(getActivity());
        adapter.setNewsData(newsData);
        listview.setAdapter(adapter);
        Call<newdata<Values1>> call = BaseApplication.httpbinService.news(1);
        call.enqueue(new Callback<newdata<Values1>>() {
            @Override
            public void onResponse(Call<newdata<Values1>> call, Response<newdata<Values1>> response) {
                for (int i = 0; i < response.body().getValues().getMtbd_ar_list().size(); i++) {
                    newsModel item=new newsModel();
                    item.setUrl(response.body().getValues().getMtbd_ar_list().get(i).getUrl());
                    item.setTitle(response.body().getValues().getMtbd_ar_list().get(i).getTitle());
                    item.setTime(response.body().getValues().getMtbd_ar_list().get(i).getAdd_time());
                    item.setId(response.body().getValues().getMtbd_ar_list().get(i).getId());
                    newsData.add(item);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<newdata<Values1>> call, Throwable t) {
            }
        });


    }

    private void initRefresh(View view){
        SmartRefreshLayout smartRefresh=view.findViewById(R.id.smart_refresh);
        smartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                smartRefresh.finishLoadMore(1000,true,false);
                int page=2;
                page++;
                Call<newdata<Values1>> news = BaseApplication.httpbinService.news(page);
                news.enqueue(new Callback<newdata<Values1>>() {
                    @Override
                    public void onResponse(Call<newdata<Values1>> call, Response<newdata<Values1>> response) {
                            for (int i = 0; i < response.body().getValues().getMtbd_ar_list().size(); i++) {
                                newsModel item = new newsModel();
                                item.setUrl(response.body().getValues().getMtbd_ar_list().get(i).getUrl());
                                item.setTitle(response.body().getValues().getMtbd_ar_list().get(i).getTitle());
                                item.setTime(response.body().getValues().getMtbd_ar_list().get(i).getAdd_time());
                                newsData.add(item);
                            }
                            adapter.setNewsData(newsData);
                            adapter.notifyItemRangeInserted(newsData.size(),response.body().getValues().getMtbd_ar_list().size());
                    }
                    @Override
                    public void onFailure(Call<newdata<Values1>> call, Throwable t) {
                    }
                });
            }
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                smartRefresh.finishRefresh(1000,true,false);
                Toast.makeText(getActivity(),"刷新成功",Toast.LENGTH_SHORT).show();
            }
        });
    }


}
